package org.springblade.modules.restaurant.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.base.TenantEntity;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.modules.restaurant.constant.RestaurantConstant;
import org.springblade.modules.restaurant.entity.UserSalary;
import org.springblade.modules.restaurant.service.IUserSalaryService;
import org.springblade.modules.restaurant.vo.RestaurantUserVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDictService;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.DictVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author harry
 * @公众号 Harry技术
 */
@RestController
@AllArgsConstructor
@RequestMapping("/restaurant/common")
@Tag(name = "通用接口", description = "通用接口")
public class RestaurantCommonController extends BladeController {
	private IUserService userService;
	private IUserSalaryService userSalaryService;
	private IDictService dictService;

	@GetMapping("/user-list")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "用户列表")
	public R<List<RestaurantUserVO>> userList() {
		String tenantId = getUser().getTenantId();
		List<User> list = userService.lambdaQuery().eq(TenantEntity::getTenantId, tenantId).list();
		if (Func.isEmpty(list)) {
			return R.data(new ArrayList<>());
		}
		return R.data(BeanUtil.copyToList(list, RestaurantUserVO.class));
	}

	@GetMapping("/userListBySalary")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "用户列表(添加禁用属性)")
	public R<List<RestaurantUserVO>> userListBySalary(Boolean disabled) {
		String tenantId = getUser().getTenantId();
		List<User> list = userService.lambdaQuery().eq(TenantEntity::getTenantId, tenantId).list();
		if (Func.isEmpty(list)) {
			return R.data(new ArrayList<>());
		}
		List<RestaurantUserVO> userList = new ArrayList<>();
		list.forEach(user -> {
			RestaurantUserVO restaurantUserVO = new RestaurantUserVO();
			BeanUtil.copyProperties(user, restaurantUserVO);
			restaurantUserVO.setLabel(user.getRealName());
			restaurantUserVO.setValue(user.getId());
			userList.add(restaurantUserVO);
		});

		if (ObjectUtil.isEmpty(disabled) || disabled) {
			List<Long> userSalaryIds = userSalaryService.list().stream().map(UserSalary::getUserId).toList();
			// 如果存在 则禁止选择
			userList.forEach(user -> {
				if (userSalaryIds.contains(user.getValue())) {
					user.setDisabled(true);
				}
			});
		}

		return R.data(userList);
	}

	// 获取订台用户
	@GetMapping("/userListByBooker")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "订台用户列表")
	public R<List<RestaurantUserVO>> userListByBooker() {
		String tenantId = getUser().getTenantId();
		List<RestaurantUserVO> userList = new ArrayList<>();
		List<UserSalary> list = userSalaryService.list(Wrappers.<UserSalary>lambdaQuery()
			.eq(UserSalary::getIsBooker, 1)
			.eq(TenantEntity::getTenantId, tenantId));

		list.forEach(item -> {
			RestaurantUserVO restaurantUserVO = new RestaurantUserVO();
			restaurantUserVO.setId(item.getUserId());
			restaurantUserVO.setLabel(item.getUserName());
			restaurantUserVO.setValue(item.getUserId());
			restaurantUserVO.setAccount(item.getUserName());
			restaurantUserVO.setRealName(item.getUserName());
			restaurantUserVO.setMonthlySalary(item.getMonthlySalary());
			restaurantUserVO.setDailySalary(item.getDailySalary());
			userList.add(restaurantUserVO);
		});
		return R.data(userList);
	}

	// 获取看台用户
	@GetMapping("/userListByStand")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "看台用户列表")
	public R<List<RestaurantUserVO>> userListByStand() {
		String tenantId = getUser().getTenantId();
		List<RestaurantUserVO> userList = new ArrayList<>();
		List<UserSalary> list = userSalaryService.list(Wrappers.<UserSalary>lambdaQuery()
			.eq(UserSalary::getIsStand, 1)
			.eq(TenantEntity::getTenantId, tenantId));

		list.forEach(item -> {
			RestaurantUserVO restaurantUserVO = new RestaurantUserVO();
			restaurantUserVO.setId(item.getUserId());
			restaurantUserVO.setLabel(item.getUserName());
			restaurantUserVO.setValue(item.getUserId());
			restaurantUserVO.setAccount(item.getUserName());
			restaurantUserVO.setRealName(item.getUserName());
			restaurantUserVO.setMonthlySalary(item.getMonthlySalary());
			restaurantUserVO.setDailySalary(item.getDailySalary());
			userList.add(restaurantUserVO);
		});
		return R.data(userList);
	}


	// 获取分类树
	@GetMapping("/getLevelTree")
	@Operation(summary = "分类树", description = "分类树")
	public R<List<DictVO>> getLevelTree() {
		List<DictVO> list = dictService.treeByCodePrefix(RestaurantConstant.FIXED_EXPENSES);
		return R.data(list);
	}

	// 获取参与考勤的用户
	@GetMapping("/userAttendance")
	@Operation(summary = "参与考勤的用户", description = "参与考勤的用户")
	public R<List<RestaurantUserVO>> userAttendance() {
		String tenantId = getUser().getTenantId();
		List<RestaurantUserVO> userList = new ArrayList<>();
		List<UserSalary> list = userSalaryService.list(Wrappers.<UserSalary>lambdaQuery().eq(TenantEntity::getTenantId, tenantId));
		list.forEach(item -> {
			RestaurantUserVO restaurantUserVO = new RestaurantUserVO();
			restaurantUserVO.setId(item.getUserId());
			restaurantUserVO.setLabel(item.getUserName());
			restaurantUserVO.setValue(item.getUserId());
			restaurantUserVO.setAccount(item.getUserName());
			restaurantUserVO.setRealName(item.getUserName());
			restaurantUserVO.setMonthlySalary(item.getMonthlySalary());
			restaurantUserVO.setDailySalary(item.getDailySalary());
			userList.add(restaurantUserVO);
		});
		return R.data(userList);
	}

}
